#
# 【输出器】
#
# 对外提供两个方法：
#
# 1. add_data(data)
# 存储网页数据，接收一个参数 data
#
# 2. output()
# 将数据输出到 html 文件中
#
from sam import export_csv_path
from sam.util import csvUtil


class Output(object):
    def __init__(self):
        super().__init__()
        self.data_list = []

    def add_data(self, data):
        # if data is None:
        #     return
        if data:
            self.data_list.append(data)

    def output2(self):
        head = '''
    <html>
    <meta charset="UTF-8">
    <body>
        <table>
                 '''

        foot = '''
        </table>
    </body>
    </html>
                 '''
        with open('CSDN.html', 'w', encoding="utf-8") as f:
            f.write(head)
            for data in self.data_list:
                f.write(f"""
                <tr>
                    <td>
                        <a href ={data['url']}>{data['url']}</a>
                    </td>
                    <td>{data['title']}</td>
                </tr>
                    """)
            f.write(foot)

    def output(self):
        csvUtil.csv_write(export_csv_path, self.data_list)
